/*
 * @Author: szx
 * @Date: 2021-12-03 13:16:24
 * @LastEditTime: 2021-12-03 13:21:49
 * @Description:
 * @FilePath: \leetcode\900-999\977\977.js
 */
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var sortedSquares = function (nums) {
    const n = nums.length;
    let l = 0;
    r = n - 1;
    const res = new Array(n).fill(0);
    let i = n - 1;
    while (i >= 0) {
        if (Math.abs(nums[l]) > Math.abs(nums[r])) {
            res[i--] = nums[l] * nums[l];
            l++;
        } else {
            res[i--] = nums[r] * nums[r];
            r--;
        }
    }
    return res;
};
